
* Append marriage data

* 1998-2004

forvalues t=1998(1)2004{
use "$mat\M_`t'.dta" , clear

rename y_marriage y_survey
generate h_agem= y_survey-h_born_y 
generate w_agem= y_survey-w_born_y 
generate delta_age = h_agem-w_agem
gen h_agecat=.
replace h_agecat =1 if h_agem >=15 & h_agem<=20
replace h_agecat =2 if h_agem >=21 & h_agem<=25
replace h_agecat =3 if h_agem >=26 & h_agem<=30
replace h_agecat =4 if h_agem >=31 & h_agem<=35
replace h_agecat =5 if h_agem >=36 & h_agem<=40
replace h_agecat =6 if h_agem >=41 & h_agem<=45
replace h_agecat =7 if h_agem >=46 
label var h_agecat "husband age"
label define age_cat 1 "15-20" 2 "21-25" 3 "26-30" 4 "31-35" 5 "36-40" 6 "41-45" 7 "more 46"
label values h_agecat age_cat
gen w_agecat=.
replace w_agecat =1 if w_agem >=15 & w_agem<=20
replace w_agecat =2 if w_agem >=21 & w_agem<=25
replace w_agecat =3 if w_agem >=26 & w_agem<=30
replace w_agecat =4 if w_agem >=31 & w_agem<=35
replace w_agecat =5 if w_agem >=36 & w_agem<=40
replace w_agecat =6 if w_agem >=41 & w_agem<=45
replace w_agecat =7 if w_agem >=46 
label var w_agecat "wife age"
label values w_agecat age_cat

rename h_citizenship h_nationality
generate h_nat=0
replace h_nat=1 if h_born_prov==888    // fg
replace h_nat=2 if h_born_prov!=888 & (h_type_citizenship==2 | h_type_citizenship==3) // sg

rename w_citizenship w_nationality
generate w_nat=0
replace w_nat=1 if w_born_prov==888  // fg
replace w_nat=2 if w_born_prov!=888 & (w_type_citizenship==2 | w_type_citizenship==3) // sg

generate h_education=.
replace h_education=1 if h_educ==5 
replace h_education=2 if h_educ==4
replace h_education=3 if h_educ==3 | h_educ==2 
replace h_education=4 if h_educ==1 
label var h_education "husb education, ascending"
label define istruzione_ascel  1 "elementare o no titolo" 2 "licenza media" 3 "licenza superiore o laurea breve" 4 "laurea" 
label values h_education istruzione_ascel

generate w_education=.
replace w_education=1 if w_educ==5
replace w_education=2 if w_educ==4
replace w_education=3 if w_educ==3 | w_educ==2
replace w_education=4 if w_educ==1 
label var w_education "wife education, ascending"
label values w_education istruzione_ascel
generate delta_edu= h_education-w_education
label var delta_edu "Education gap, H-W"					
tab h_education 
tab w_education 
sum h_education w_education delta_edu 

gen h_ey=.
replace h_ey =5 if h_educ==5
replace h_ey =8 if h_educ==4
replace h_ey =13 if h_educ==3
replace h_ey =16 if h_educ==2 // diploma univ o laurea breve 3 anni
replace h_ey =18 if h_educ==1 // laurea o dottorato ricerca 

gen w_ey=.
replace w_ey =5 if w_educ==5
replace w_ey =8 if w_educ==4
replace w_ey =13 if w_educ==3
replace w_ey =16 if w_educ==2 // diploma univ o laurea breve 3 anni
replace w_ey =18 if w_educ==1 // laurea o dottorato ricerca 

tab h_ey
tab w_ey

duplicates report
duplicates drop
duplicates drop y_survey m_marriage day_marriage mun_marriage prov_marriage regime_marriage ///
                h_born_day h_born_m h_born_y h_born_prov h_resid_prov h_marital_status h_educ h_nationality ///
				w_born_day w_born_m w_born_y w_born_prov w_resid_prov w_marital_status w_educ w_nationality , force

save "$mat\M_`t'_v2.dta" , replace

}
	 	
		  
* 2005-2006

 forvalues t=2005(1)2006{
use "$mat\M_`t'.dta" , clear
  
rename y_marriage y_survey
generate h_agem= y_survey-h_born_y 
generate w_agem= y_survey-w_born_y 
generate delta_age = h_agem-w_agem
gen h_agecat=.
replace h_agecat =1 if h_agem >=15 & h_agem<=20
replace h_agecat =2 if h_agem >=21 & h_agem<=25
replace h_agecat =3 if h_agem >=26 & h_agem<=30
replace h_agecat =4 if h_agem >=31 & h_agem<=35
replace h_agecat =5 if h_agem >=36 & h_agem<=40
replace h_agecat =6 if h_agem >=41 & h_agem<=45
replace h_agecat =7 if h_agem >=46 
label var h_agecat "husband age"
label define age_cat 1 "15-20" 2 "21-25" 3 "26-30" 4 "31-35" 5 "36-40" 6 "41-45" 7 "more 46"
label values h_agecat age_cat

gen w_agecat=.
replace w_agecat =1 if w_agem >=15 & w_agem<=20
replace w_agecat =2 if w_agem >=21 & w_agem<=25
replace w_agecat =3 if w_agem >=26 & w_agem<=30
replace w_agecat =4 if w_agem >=31 & w_agem<=35
replace w_agecat =5 if w_agem >=36 & w_agem<=40
replace w_agecat =6 if w_agem >=41 & w_agem<=45
replace w_agecat =7 if w_agem >=46 
label var w_agecat "wife age"
label values w_agecat age_cat

recode h_born_prov (0=888)
recode w_born_prov (0=888)
generate h_nat=0
replace h_nat=1 if h_born_prov==888 // fg 
replace h_nat=2 if h_born_prov!=888 & (h_type_citizenship==2 | h_type_citizenship==3) // sg

generate w_nat=0
replace w_nat=1 if w_born_prov==888 // fg 
replace w_nat=2 if w_born_prov!=888 & (w_type_citizenship==2 | w_type_citizenship==3) //sg

generate h_education=.
replace h_education=1 if (h_educ==5 & y_survey==2005) | (h_educ==4 & y_survey==2006) 
replace h_education=2 if (h_educ==4 & y_survey==2005) | (h_educ==3 & y_survey==2006)
replace h_education=3 if ((h_educ==3 | h_educ==2) & y_survey==2005) | (h_educ==2 & y_survey==2006)
replace h_education=4 if h_educ==1 
label var h_education "husb education, ascending"
label define istruzione_ascel  1 "elementare o no titolo" 2 "licenza media" 3 "licenza superiore o laurea breve" 4 "laurea" 
label values h_education istruzione_ascel

generate w_education=.
replace w_education=1 if (w_educ==5 & y_survey==2005) | (w_educ==4 & y_survey==2006) 
replace w_education=2 if (w_educ==4 & y_survey==2005) | (w_educ==3 & y_survey==2006)
replace w_education=3 if (w_educ==3 | w_educ==2 & y_survey==2005) | (w_educ==2 & y_survey==2006)
replace w_education=4 if w_educ==1 
label var w_education "wife education, ascending"
label values w_education istruzione_ascel
tab h_education 
tab w_education 
sum h_education w_education delta_edu 

gen h_ey=.
replace h_ey =5 if (h_educ==5 & y_survey==2005) | (h_educ==4 & y_survey==2006)
replace h_ey =8 if (h_educ==4 & y_survey==2005) | (h_educ==3 & y_survey==2006)
replace h_ey =15 if ((h_educ==3 | h_educ==2) & y_survey==2005) | (h_educ==2 & y_survey==2006)
replace h_ey =18 if h_educ==1 // laurea o dottorato ricerca 

gen w_ey=.
replace w_ey =5 if (w_educ==5 & y_survey==2005) | (w_educ==4 & y_survey==2006)
replace w_ey =8 if (w_educ==4 & y_survey==2005) | (w_educ==3 & y_survey==2006)
replace w_ey =15 if ((w_educ==3 | w_educ==2) & y_survey==2005) | (w_educ==2 & y_survey==2006)
replace w_ey =18 if w_educ==1 // laurea o dottorato ricerca 

tab h_ey
tab w_ey

duplicates report
duplicates drop
duplicates drop y_survey m_marriage day_marriage mun_marriage prov_marriage regime_marriage ///
                h_born_day h_born_m h_born_y h_born_prov h_resid_prov h_marital_status h_educ h_born_origin ///
				w_born_day w_born_m w_born_y w_born_prov w_resid_prov w_marital_status w_educ w_born_origin , force

				
 save "$mat\M_`t'_v2.dta" , replace

}
	  
 	  
*  2007-2012

forvalues t=2007(1)2012{
use "$mat\M_`t'.dta" , clear
	
rename y_marriage y_survey
generate h_agem= y_survey-h_born_y 
generate w_agem= y_survey-w_born_y 
generate delta_age = h_agem-w_agem
 gen h_agecat=.
 replace h_agecat =1 if h_agem >=15 & h_agem<=20
 replace h_agecat =2 if h_agem >=21 & h_agem<=25
 replace h_agecat =3 if h_agem >=26 & h_agem<=30
 replace h_agecat =4 if h_agem >=31 & h_agem<=35
 replace h_agecat =5 if h_agem >=36 & h_agem<=40
 replace h_agecat =6 if h_agem >=41 & h_agem<=45
 replace h_agecat =7 if h_agem >=46 
label var h_agecat "husband age"
label define age_cat 1 "15-20" 2 "21-25" 3 "26-30" 4 "31-35" 5 "36-40" 6 "41-45" 7 "more 46"
label values h_agecat age_cat
 gen w_agecat=.
 replace w_agecat =1 if w_agem >=15 & w_agem<=20
 replace w_agecat =2 if w_agem >=21 & w_agem<=25
 replace w_agecat =3 if w_agem >=26 & w_agem<=30
 replace w_agecat =4 if w_agem >=31 & w_agem<=35
 replace w_agecat =5 if w_agem >=36 & w_agem<=40
 replace w_agecat =6 if w_agem >=41 & w_agem<=45
 replace w_agecat =7 if w_agem >=46 
label var w_agecat "wife age"
label values w_agecat age_cat

recode h_born_prov (0=888)
generate h_nat=0
replace h_nat=1 if h_born_prov==888 // fg
replace h_nat=2 if h_born_prov!=888 & (h_type_citizenship==2 | h_type_citizenship==3) //sg

recode w_born_prov (0=888)
generate w_nat=0
replace w_nat=1 if w_born_prov==888 // fg 
replace w_nat=2 if w_born_prov!=888 & (w_type_citizenship==2 | w_type_citizenship==3) //sg 

generate h_education=.
replace h_education=1 if h_educ==5 
replace h_education=2 if h_educ==4
replace h_education=3 if h_educ==3 | h_educ==2 
replace h_education=4 if h_educ==1 
label var h_education "husb education, ascending"
label define istruzione_ascel  1 "elementare o no titolo" 2 "licenza media" 3 "licenza superiore o laurea breve" 4 "laurea" 
label values h_education istruzione_ascel

generate w_education=.
replace w_education=1 if w_educ==5
replace w_education=2 if w_educ==4
replace w_education=3 if w_educ==3 | w_educ==2
replace w_education=4 if w_educ==1 
label var w_education "wife education, ascending"
label values w_education istruzione_ascel
generate delta_edu= h_education-w_education
label var delta_edu "Education gap, H-W"					

tab h_education 
tab w_education 
sum h_education w_education delta_edu 

gen h_ey=.
replace h_ey =5 if h_educ==5
replace h_ey =8 if h_educ==4
replace h_ey =13 if h_educ==3
replace h_ey =16 if h_educ==2 // diploma univ o laurea breve 3 anni
replace h_ey =18 if h_educ==1 // laurea o dottorato ricerca 

gen w_ey=.
replace w_ey =5 if w_educ==5
replace w_ey =8 if w_educ==4
replace w_ey =13 if w_educ==3
replace w_ey =16 if w_educ==2 // diploma univ o laurea breve 3 anni
replace w_ey =18 if w_educ==1 // laurea o dottorato ricerca 

tab h_ey
tab w_ey


duplicates report
duplicates drop
duplicates drop y_survey m_marriage day_marriage mun_marriage prov_marriage regime_marriage ///
                h_born_day h_born_m h_born_y h_born_prov h_resid_prov h_marital_status h_educ h_born_origin ///
				w_born_day w_born_m w_born_y w_born_prov w_resid_prov w_marital_status w_educ w_born_origin , force

				
save "$mat\M_`t'_v2.dta" , replace
}
	  
	  

*  Append all marriage years in the final dataset

use  "$mat\M_1998_v2.dta" , clear
 
forvalues t=1999/2012{
append using "$mat\M_`t'_v2.dta"
}

* Origin of immigrants 

gen h_born_c = 0 if y_survey>=1998 & y_survey<=2004
replace h_born_c= h_born_provita if h_born_provita>200 & y_survey>=1998 & y_survey<=2004
gen hbc=.
replace hbc= h_born_c if  y_survey>=1998 & y_survey<=2004
replace hbc= h_nationality if y_survey>=1998 & y_survey<=2004 & h_nat==2 //sg
replace hbc= h_born_origin if y_survey>=2005 
replace hbc= h_citizenship if y_survey>=2005 & h_nat==2 // sg
lab var hbc "H country of origin e SG"

gen w_born_c = 0 if y_survey>=1998 & y_survey<=2004
replace w_born_c= w_born_provita if w_born_provita>200 & y_survey>=1998 & y_survey<=2004
gen wbc=.
replace wbc= w_born_c if  y_survey>=1998 & y_survey<=2004
replace wbc= w_nationality if  y_survey>=1998 & y_survey<=2004 & w_nat==2 //sg
replace wbc= h_born_origin if y_survey>=2005 
replace wbc= w_citizenship if y_survey>=2005 & w_nat==2 // sg
lab var wbc "W country of origin e SG"

drop if hbc==999 // apolide
drop if wbc==999 // apolide
recode hbc (270 = 224 ) // Serbia e Montenegro insieme
recode hbc (271 = 224 )
recode wbc (270 = 224 ) // Serbia e Montenegro insieme
recode wbc (271 = 224 )


* Geographical location (province fo reference)

cap drop provres
gen provres = .
replace provres = h_resid_prov if y_survey<2007 & hres_out!=1
replace provres = w_resid_prov if y_survey<2007 & hres_out==1
replace provres = h_resid_prov_f if y_survey>=2007 & hres_out!=1 & h_resid_prov_f!=0
replace provres = w_resid_prov_f if y_survey>=2007 & hres_out==1 & w_resid_prov_f!=0
replace provres = h_resid_prov if y_survey>=2007 & hres_out!=1 & h_resid_prov_f==0 & h_resid_prov!=0
replace provres = w_resid_prov if y_survey>=2007 & hres_out==1 & w_resid_prov_f==0 & w_resid_prov!=0
lab var provres "provincia residenza"
lab values provres codici_province
tab provres

* Recode provinces that change over time

recode provres ( 108 = 15) // change monza-brianza in MILANO
recode provres ( 110 = 72) // change barletta-andria-trani e foggia in BARI 
recode provres  ( 71 = 72)  
recode provres ( 91 = 92 ) // change oristano, nuoro, olbia-tempio, ogliastra, medio-campidano, carbonia-iglesias in CAGLIARI
recode provres ( 95 = 92 )
recode provres ( 104 = 92 )
recode provres ( 105 = 92 )
recode provres ( 106 = 92 )
recode provres ( 107 = 92 )
recode provres ( 99 = 41)  // change rimini in PESARO
recode provres ( 109 = 44) // change fermo in ASCOLI

cap drop region 
gen region = .
replace region=1 if provres==1 | provres==2 | provres==3 | provres==4 | provres==5 | provres==6 | provres==96 | provres==103
replace region=2 if provres==7
replace region=3 if provres==12 | provres==13 | provres==14 | provres==15 | provres==16 | provres==17 | provres==18 | provres==19 | provres==20 | provres==97 | provres==98 | provres==108
replace region=4 if provres==21 | provres==22
replace region=5 if provres==23 | provres==24 | provres==25 | provres==26 | provres==27 | provres==28 | provres==29
replace region=6 if provres==30 | provres==31 | provres==32 | provres==93 
replace region=7 if provres==8 | provres==9 | provres==10 | provres==11 
replace region=8 if provres==33 | provres==34 | provres==35 | provres==36 | provres==37 | provres==38 | provres==39 | provres==40 | provres==99
replace region=9 if provres==45 | provres==46 | provres==47 | provres==48 | provres==49 | provres==50 | provres==51 | provres==52 | provres==53 | provres==100
replace region=10 if provres==54 | provres==55 
replace region=11 if provres==41 | provres==42 | provres==43 | provres==44 
replace region=12 if provres==109 | provres==56 | provres==57 | provres==58 | provres==59 | provres==60 
replace region=13 if provres==66 | provres==67 | provres==68 | provres==69  
replace region=14 if provres==70 | provres== 94 
replace region=15 if provres==61 | provres==62 | provres==63 | provres==64 | provres==65 
replace region=16 if provres==71 | provres==72 | provres==73 | provres==74 | provres==75 | provres==110 
replace region=17 if provres==76 | provres==77 
replace region=18 if provres==78 | provres==79 | provres==80 | provres==101 | provres==102 
replace region=19 if provres==81 | provres==82 | provres==83 | provres==84 | provres==85 | provres==86 | provres==87 | provres==88 | provres==89 
replace region=20 if provres==90 | provres==91 | provres==92 | provres==95 | provres==104 | provres==105 | provres==106 | provres==107

cap drop region_marriage
gen region_marriage = .
replace region_marriage=1 if prov_marriage==1 | prov_marriage==2 | prov_marriage==3 | prov_marriage==4 | prov_marriage==5 | prov_marriage==6 | prov_marriage==96 | prov_marriage==103
replace region_marriage=2 if prov_marriage==7
replace region_marriage=3 if prov_marriage==12 | prov_marriage==13 | prov_marriage==14 | prov_marriage==15 | prov_marriage==16 | prov_marriage==17 | prov_marriage==18 | prov_marriage==19 | prov_marriage==20 | prov_marriage==97 | prov_marriage==98 | prov_marriage==108
replace region_marriage=4 if prov_marriage==21 | prov_marriage==22
replace region_marriage=5 if prov_marriage==23 | prov_marriage==24 | prov_marriage==25 | prov_marriage==26 | prov_marriage==27 | prov_marriage==28 | prov_marriage==29
replace region_marriage=6 if prov_marriage==30 | prov_marriage==31 | prov_marriage==32 | prov_marriage==93 
replace region_marriage=7 if prov_marriage==8 | prov_marriage==9 | prov_marriage==10 | prov_marriage==11 
replace region_marriage=8 if prov_marriage==33 | prov_marriage==34 | prov_marriage==35 | prov_marriage==36 | prov_marriage==37 | prov_marriage==38 | prov_marriage==39 | prov_marriage==40 | prov_marriage==99
replace region_marriage=9 if prov_marriage==45 | prov_marriage==46 | prov_marriage==47 | prov_marriage==48 | prov_marriage==49 | prov_marriage==50 | prov_marriage==51 | prov_marriage==52 | prov_marriage==53 | prov_marriage==100
replace region_marriage=10 if prov_marriage==54 | prov_marriage==55 
replace region_marriage=11 if prov_marriage==41 | prov_marriage==42 | prov_marriage==43 | prov_marriage==44 
replace region_marriage=12 if prov_marriage==109 | prov_marriage==56 | prov_marriage==57 | prov_marriage==58 | prov_marriage==59 | prov_marriage==60 
replace region_marriage=13 if prov_marriage==66 | prov_marriage==67 | prov_marriage==68 | prov_marriage==69  
replace region_marriage=14 if prov_marriage==70 | prov_marriage== 94 
replace region_marriage=15 if prov_marriage==61 | prov_marriage==62 | prov_marriage==63 | prov_marriage==64 | prov_marriage==65 
replace region_marriage=16 if prov_marriage==71 | prov_marriage==72 | prov_marriage==73 | prov_marriage==74 | prov_marriage==75 | prov_marriage==110 
replace region_marriage=17 if prov_marriage==76 | prov_marriage==77 
replace region_marriage=18 if prov_marriage==78 | prov_marriage==79 | prov_marriage==80 | prov_marriage==101 | prov_marriage==102 
replace region_marriage=19 if prov_marriage==81 | prov_marriage==82 | prov_marriage==83 | prov_marriage==84 | prov_marriage==85 | prov_marriage==86 | prov_marriage==87 | prov_marriage==88 | prov_marriage==89 
replace region_marriage=20 if prov_marriage==90 | prov_marriage==91 | prov_marriage==92 | prov_marriage==95 | prov_marriage==104 | prov_marriage==105 | prov_marriage==106 | prov_marriage==107

* Local labor markets
cap drop istat
gen istat = .
replace istat = h_COMUNE_RES if hres_out!=1
replace istat = w_COMUNE_RES if hres_out==1
lab var istat "municipality - residenza"

* Control for out migration 

gen hres_out=.
replace hres_out =1 if h_resid_prov>111 & y_survey>=1998 & y_survey<=2004 
replace hres_out =1 if h_resid_prov==0  & y_survey>=2005 & y_survey<=2012 
tab hres_out
gen wres_out=.
replace wres_out =1 if w_resid_prov>111 & y_survey>=1998 & y_survey<=2004
replace wres_out =1 if w_resid_prov==0  & y_survey>=2005 & y_survey<=2012 
tab wres_out
count if wres_out==1 & hres_out==1 // 77911 obs
gen out= 1 if wres_out==1 & hres_out==1
drop if out==1

count
save "$data\M_final_dataset.dta" , replace
 
 

